using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using TTQT.TTQT_Code;

public static class DAL
{
    public static SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
    //  new SqlConnection("server=10.53.18.11\\DB_SQL_2005;database=HR2;uid=sa;pwd=sa");
    public static  DataSet FillDataset(ref string err, SqlDataAdapter adt)
    {
        DataSet ds = new DataSet();
        adt.SelectCommand.Connection = conn;
        try
        {
            adt.Fill(ds);
        }
        catch (Exception ex)
        {
            err = ex.Message;
        }
        return ds;
    }
    public static void ExecCommand(ref string err, SqlCommand cmd)
    {
        cmd.Connection = conn;
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            err = ex.Message;
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }

    public static int ExecCommandScalar(ref string err, SqlCommand cmd)
    {
        cmd.Connection = conn;
        int i=0;
        try
        {
            conn.Open();
            i= TTQTCommon.ToInt32(cmd.ExecuteScalar(), -1);
        }
        catch (Exception ex)
        {
            err = ex.Message;
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
        return i;
    }
}


